//引入异步请求
import { request } from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    communityName: '',
    communityNames: [
      {
        communityName: '大宅风范城',
        communityId: '0'
      },
      {
        communityName: '嘉定新航域',
        communityId: '1'
      },
      {
        communityName: '江桥万达',
        communityId: '2'
      },
      {
        communityName: '外滩',
        communityId: '3'
      }
    ],
    show: false,
    //switch开关
    checked: false,
    //定义地址信息数组
    addressArr: [],
    //当前要修改的地址信息
    addressObj: {}
  },
  //地址信息对象
  addressObj: {},
  //保存地址时传递到服务器的参数
  QueryParams: {
    //社区地址
    addressCommunity: "",
    //社区名称
    addressCommunityName: "",
    //详细地址
    addressDetails: "",
    //联系人
    addressContacts: "",
    //联系电话
    addressContactsNumber: "",
    //是否默认
    addressDefault: "",
    //传递过来的地址id
    addressId: ''
  },
  //保存修改地址传递进来的地址id
  addressId: '',
  //显示弹出层
  showPopup() {
    this.setData({
      show: true
    })
  },
  //关闭弹出层
  closePopup() {
    this.setData({
      show: false
    })
  },
  //选择社区时触发
  handleChooseCommunity(e) {
    const { index } = e.currentTarget.dataset
    this.setData({
      communityName: this.data.communityNames[index].communityName
    })
  },
  //switch开关的控制
  onChange({ detail }) {
    // console.log(detail)
    // let { addressArr } = this.data
    // addressArr.forEach(v => v.addressDefault = false)
    const { addressObj, checked } = this.data
    if (!this.addressId) {
      this.setData({
        checked: !checked
      })
      this.addressObj.addressDefault = !checked
    } else {
      addressObj.addressDefault = detail
      this.setData({
        addressObj
      })
    }
  },
  //点击保存按钮事件
  async handleSaveAddress() {
    if (this.addressId) {
      let token = wx.getStorageSync("token");
      const header = { token }
      const addressId = this.addressId
      const { addressObj } = this.data
      this.QueryParams.addressId = addressId
      this.QueryParams.addressCommunity = addressObj.addressCommunity
      this.QueryParams.addressCommunityName = addressObj.addressCommunityName
      this.QueryParams.addressDetails = addressObj.addressDetails
      this.QueryParams.addressContacts = addressObj.addressContacts
      this.QueryParams.addressContactsNumber = addressObj.addressContactsNumber
      this.QueryParams.addressDefault = addressObj.addressDefault
      console.log(this.QueryParams)
      const res = await request({ url: '/user/address/' + addressId, method: 'PUT', data: this.QueryParams, header })
      console.log(res)
      wx.navigateBack({
        delta: 1
      });
    } else {
      let { addressArr } = this.data
      this.addressObj.addressId = addressArr.length
      this.addressObj.communityName = this.data.communityName
      addressArr.push(this.addressObj)
      this.setData({
        addressArr
      })
      let token = wx.getStorageSync("token");
      const header = { token }
      this.QueryParams.addressCommunity = 1
      this.QueryParams.addressCommunityName = this.addressObj.communityName
      this.QueryParams.addressDetails = this.addressObj.all
      this.QueryParams.addressContacts = this.addressObj.userName
      this.QueryParams.addressContactsNumber = this.addressObj.telNumber
      this.QueryParams.addressDefault = this.addressObj.addressDefault
      console.log(this.QueryParams)
      const res = await request({ url: '/user/address', method: 'POST', data: this.QueryParams, header })
      //console.log(res)
      wx.setStorageSync("addressArr", addressArr);
      wx.navigateBack({
        delta: 1
      });
    }

  },
  //获取收货人姓名
  getUserName(e) {
    this.addressObj.userName = e.detail
  },
  getPhoneNum(e) {
    this.addressObj.telNumber = e.detail
  },
  getPhone(e) {
    this.addressObj.phoneNumber = e.detail
  },
  getAddressDetail(e) {
    this.addressObj.all = this.data.communityName + e.detail
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // const res = await getSetting()
    // const userInfo = res.authSetting["scope.userInfo"]
    // if (!userInfo) {
    //   wx.navigateTo({
    //     url: '../login/index',
    //     success: (result) => {

    //     }
    //   });

    // }
    // const { addressId } = options
    // let { addressArr } = this.data
    // const index = addressArr.findIndex(v => v.addressId === addressId)
    // this.setData({
    //   addressObj: addressArr[index]
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    // let addressArr = wx.getStorageSync("addressArr") || [];
    // //获取当前小程序的页面栈-内存中的数组  长度 最大是10页面   连续跳转
    // //数组中索引最大的页面 就是当前页面
    // let pages = getCurrentPages();
    // let currentPage = pages[pages.length - 1]
    // const { addressId } = currentPage.options
    // const index = addressArr.findIndex(v => v.addressId === parseInt(addressId))
    // this.setData({
    //   addressArr,
    //   addressObj: addressArr[index]
    // })
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1]
    let { addressId } = currentPage.options
    this.setData({
      addressId
    })
    if (addressId) {
      addressId = parseInt(addressId)
      console.log(addressId)
      this.addressId = addressId
      let token = wx.getStorageSync("token");
      const header = { token }
      const res = await request({ url: '/user/address/' + addressId + '/editInfo', data: { addressId }, header })
      console.log(res)
      this.setData({
        addressObj: res.data
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})